import { WeaTop,WeaTab,WeaButtonIcon,WeaAuth,WeaTools} from 'ecCom';
import {inject, observer} from 'mobx-react';
import {WeaLocaleProvider} from 'ecCom';
const getLabel = WeaLocaleProvider.getLabel;
import {Button,Modal,} from 'antd';
import * as MeetingShareAPI from '../../../../apis/meetingShare'
import {WeaTableNew} from 'comsMobx'
const WeaTable = WeaTableNew.WeaTable;
import {toJS} from 'mobx'

@inject('meetingShare')
@observer
class MeetingShare extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      conditions: [],
      authVisible: false,
    }
  }

  componentDidMount() {
    // 初始化请求数据
    const {meetingShare} = this.props;
    meetingShare.doGetShareTable(this.props.meetingId);
  }
  componentWillReceiveProps(nextProps) {
  }
  componentWillUnmount() {
	  //组件卸载时一般清理一些状态
  }

  render() {
    const {meetingShare} = this.props;
    let  {columns,dataSource,dataKey,comsWeaTableStore,onlyCanView} = meetingShare;
    let {conditions,authVisible} = this.state;
    const tableNow = comsWeaTableStore;
    return (
      <div>
        <WeaTab ecId={`${this && this.props && this.props.ecId || ''}_WeaTab@lr7e93`}
          keyParam="viewcondition"
          countParam="groupid"
          buttons={[
            !onlyCanView&&<WeaButtonIcon ecId={`${this && this.props && this.props.ecId || ''}_WeaButtonIcon@qx95uk@add`} buttonType='add' type='primary' disabled={false} size='small' onClick={this.addShare}/>,
            !onlyCanView&&<WeaButtonIcon ecId={`${this && this.props && this.props.ecId || ''}_WeaButtonIcon@s6swjv@del`} buttonType='del' type='primary' disabled={tableNow && tableNow.selectedRowKeys.length>0 ? false:true}  onClick={this.deletedShare}/>,
          ]}
        />
        <WeaTable ecId={`${this && this.props && this.props.ecId || ''}_WeaTable@ki4lit`} 
          sessionkey={dataKey}
          comsWeaTableStore={comsWeaTableStore}
          hasOrder={true}
          needScroll={false}
          onOperatesClick={this.onOperatesClick}
          />
        <WeaAuth ecId={`${this && this.props && this.props.ecId || ''}_WeaAuth@qq4w5u`} 
          visible={authVisible}
          conditions={conditions}
          icon={'icon-coms-meeting'}
          iconBgcolor={'#f14a2d'}
          onOk={v => this.onOk(v)}
          onCancel={() => this.onCancel()}
        />
      </div>
    );
  }
  
  onOperatesClick = (record,index,operate,flag) => {
    let {meetingId} = this.props;
    const {doDeleteById} = this.props.meetingShare;
    record = toJS(record);
    let {id} = record;
    let values = {delIds:id};
  
    Modal.confirm({
      title: `${getLabel(131329,'信息确认')}`,
      content: `${getLabel(15097,'确定要删除吗？')}`,
      onOk() {
        doDeleteById(values,meetingId);
      },
      onCancel() {},
    }) 
  }

  // 添加共享
  addShare = () => {
    MeetingShareAPI.getDatas().then((result)=>{  
      this.setState({conditions:result.fields[0].items,authVisible:true});
      let obj = {conditions:result, authVisible:true};
      // this.props.callbackState(obj)
		})
  }

  // 删除共享
  deletedShare = () => {
    let _this = this;
    const {doDeleteById} = this.props.meetingShare;

    let {dataKey,comsWeaTableStore,doGetShareTable} = this.props.meetingShare;
    let {meetingId} = this.props;
    comsWeaTableStore = toJS(comsWeaTableStore);
    const tableNow = comsWeaTableStore;
    if(tableNow && tableNow.selectedRowKeys.length == 0){
      Modal.warning({
        title: `${getLabel(127354,'警告')}`,
        content: `${getLabel(32568,'请选择要删除的记录!')}`,
      })
    }else if(tableNow && tableNow.selectedRowKeys.length > 0){
      let keys = tableNow.selectedRowKeys.join(',');
      let values = {delIds:keys};
      Modal.confirm({
        title: `${getLabel(131329,'信息确认')}`,
        content: `${getLabel(15097,'确定要删除吗?')}`,
        onOk() {
          doDeleteById(values,meetingId);
        },
        onCancel() {}
      })
    }
  }
  
  onOk = (v) => {
    let values = {...v, meetingId:this.props.meetingId};
    const {doGetShareTable} = this.props.meetingShare;
    MeetingShareAPI.addMeetingShare(values).then((result) => {
      result.status && this.onCancel();
      result.status && doGetShareTable(this.props.meetingId);
    })
  }

  // 取消添加共享
  onCancel = () => {
    this.setState({conditions:[],authVisible: false});
  }

}

export default MeetingShare;